package com.juzhencms.apps.busi.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;

@Service
public class NewsService extends CommonService{
	
	/*public List listData(Map param, HttpServletRequest request){
		
			List sqlParamList=new ArrayList();
			
			String sql="select id,name,image,large_image,create_time,source,hits,description,menu_id,link_url from "+DBTable.news+" where 1 ";
		
			if(param.get("menu_id")!=null){
				
				sql+="and menu_id=? ";
				sqlParamList.add(param.get("menu_id"));

			}else if(param.get("tpl_flag")!=null){
				
				String tpl_flag=param.get("tpl_flag").toString();
				//将模板的tpl_flag参数转换为menu_id
				Map site=(Map)request.getAttribute("localSite");
				if(site!=null && site.get("id")!=null){
					
					
					Map menu=this.getJdbcDao().queryRow("select id,alias from "+DBTable.menu+" where site_id=? and tpl_flag=?", new Object[]{site.get("id"),tpl_flag});
					if(menu!=null){
						sql+="and menu_id=? ";
						sqlParamList.add(menu.get("id"));
					}
				}else{
					return null;
				}
			}
			
			//如果需要图片
			if(param.get("site_id")!=null&& !param.get("site_id").equals("")){
				sql+=" and site_id =? ";
				sqlParamList.add(param.get("site_id"));
			}
			
			//如果需要图片
			if(param.get("language")!=null&& !param.get("language").equals("")){
				String langeuage=param.get("language").toString();
				if(langeuage.equals("TraditionalChinese"))
					langeuage="SimplifiedChinese";
				//获取同语言所有站点
				List siteList=this.getJdbcDao().querySql("select id from "+DBTable.site+" where language=?", new Object[]{langeuage});
				StringBuffer tmpStr=new StringBuffer();
				if(siteList!=null)
					for(int i=0;i<siteList.size();i++){
						Map tmpSite=(Map)siteList.get(i);
						if(i<siteList.size()-1)
							tmpStr.append(tmpSite.get("id")+",");
						else
							tmpStr.append(tmpSite.get("id"));
					}
				else
					tmpStr.append("0");
				//查询匹配的栏目
				//LogTool.info("select id from "+DBTable.menu+" where alias='"+alias+"' and site_id in ("+tmpStr.toString()+")", this.getClass());
				List tmpMenuList=this.getJdbcDao().querySql("select id from "+DBTable.menu+" where site_id in ("+tmpStr.toString()+") and module='news'");
				
				tmpStr=new StringBuffer();
				if(tmpMenuList!=null && tmpMenuList.size()>0)
					for(int i=0;i<tmpMenuList.size();i++){
						Map tmpMenu=(Map)tmpMenuList.get(i);
						if(i<tmpMenuList.size()-1)
							tmpStr.append(tmpMenu.get("id")+",");
						else
							tmpStr.append(tmpMenu.get("id"));
					}
				else
					tmpStr.append("0");
				sql+="and menu_id in("+tmpStr.toString()+") ";
			}
			
			
			
			//图片
			if(param.get("image")!=null){
				int image=new Integer(param.get("image").toString());
				if(image>0){
					sql+=" and (image is not null and image!='') ";
				}else{
					sql+=" and (image is null or image='') ";
				}	
			}
			
			//再次查询过滤重复id
			if(param.get("no_ids")!=null && !param.get("no_ids").equals("")){
				sql+=" and id not in ("+param.get("no_ids")+")  ";
			}
			
			//查询推荐位置新闻
			if (param.get("index_recommend")!=null&& !param.get("recolocation").equals("")) {
				sql+=" and index_recommend = ?  ";
				sqlParamList.add(param.get("index_recommend"));
			}
			
			//查询推荐位置新闻
			if (param.get("recommend")!=null&& !param.get("recommend").equals("")) {
				sql+=" and recommend = ?  ";
				sqlParamList.add(param.get("recommend"));
			}

			
			sql+=" and del_flag=0 and publish=1 ";
			
			if(param.get("order")!=null){
				sql+=" order by "+param.get("order")+",create_time desc,id desc";
			}else{
				sql+=" order by recommend desc , position asc ,create_time desc,id desc";
			}
			
			int num=15;
			if(param.get("num") != null){
				num=new Integer(param.get("num").toString());
				sql+=" limit 0,?";
				sqlParamList.add(num);
			}
			

			
			List dataList = this.getJdbcDao().querySql(sql, sqlParamList.toArray());
			
			
			if(dataList!=null && dataList.size() > 0){
				for(int i=0;i<dataList.size();i++){
					Map row=(Map)dataList.get(i);
					row.put("link", this.getLink(row));
					
					if(row.get("image")==null || row.get("image").equals("")) {
						row.put("image","nophoto.jpg");
					}
					row.put("image", this.getConfig().getUploadUrlPath()+"/"+row.get("image"));
				}
			}
			
			return dataList;
	}*/

	
	/*public List pageListData(Map param, HttpServletRequest request) {
		
		//如果有关键词搜索 
		
		int page=1;
		if(param.get("page")!=null){
			page=new Integer(param.get("page").toString());
		}
		int pagesize=10;//默认10
		if(param.get("pagesize")!=null){
			pagesize=new Integer(param.get("pagesize").toString());
		}
		
		
		List sqlParamList=new ArrayList();
		
		String sql="select id,name,image,large_image,source,create_time,description,menu_id,link_url from "+DBTable.news+" where del_flag=0 ";
	
		if(param.get("menu_id")!=null){
			
			
			sql+="and menu_id=? ";
			sqlParamList.add(param.get("menu_id"));
			
			
			
			
		}else if(param.get("tpl_flag")!=null){
			
			String tpl_flag=param.get("tpl_flag").toString();
			
			//将模板的tpl_flag参数转换为menu_id
			Map site=(Map)request.getAttribute("localSite");
			if(site!=null && site.get("id")!=null){
				
				
				
				Map menu=this.getJdbcDao().queryRow("select id from "+DBTable.menu+" where site_id=? and tpl_flag=?", new Object[]{site.get("id"),tpl_flag});
				if(menu!=null){
					sql+="and menu_id=? ";
					sqlParamList.add(menu.get("id"));
				}
				
				
				
			}else{
				return null;
			}
		}
		
		//如果需要
		if(param.get("site_id")!=null&& !param.get("site_id").equals("")){
			sql+=" and site_id =? ";
			sqlParamList.add(param.get("site_id"));
		}
		
		//如果有关键词搜索 
		if(param.get("keywords")!=null&& !param.get("keywords").equals("")){
			sql+=" and name like ? ";
			sqlParamList.add("%"+param.get("keywords")+"%");
		}
		
		//如果需要图片
		if(param.get("image")!=null&& !param.get("image").equals("")){
			sql+=" and image is not null and image!='' ";
		}
		//再次查询过滤重复id
		if(param.get("no_ids")!=null&& !param.get("no_ids").equals("")){
			sql+=" and id  not in ("+param.get("no_ids")+")";
		}
		//查询推荐位置新闻
		if (param.get("recolocation")!=null&& !param.get("recolocation").equals("")) {
			sql+=" and recommend_location like ?  ";
			sqlParamList.add("%"+param.get("recolocation")+"%");
		}
		
		sql+=" and publish=1 ";
		
		if(param.get("order")!=null&& !param.get("order").equals("")){
			sql+=" order by "+param.get("order")+",create_time desc,id desc";
		}else{
			sql+=" order by recommend desc , position asc ,create_time desc,id desc";
		}
		
		int beginNum=(page-1)*pagesize;
		int endnum=pagesize;
		sql+=" limit ?,?";
		sqlParamList.add(beginNum);
		sqlParamList.add(pagesize);
		
		
		List dataList = this.getJdbcDao().querySql(sql, sqlParamList.toArray());
		
		
		if(dataList!=null && dataList.size() > 0){
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				row.put("link", this.getLink(row));
				
				if(row.get("image")==null || row.get("image").equals("")) {
					row.put("image","nophoto.jpg");
				}
				row.put("image", this.getConfig().getUploadUrlPath()+"/"+row.get("image"));
				
			}
		}
		
		return dataList;
	}*/


	
	public Map detailData(Map param){
		int news_id=Integer.parseInt((String) (param.get("news_id").equals(null)?"":param.get("news_id")));
		Map record=this.getJdbcDao().queryRow("select * from "+DBTable.news+" where id=?",new Object[]{news_id});
		String site_id = record.get("site_id").toString();
		Map siteRecord=this.getJdbcDao().queryRow("select * from "+DBTable.site+" where id=?",new Object[]{site_id});
		record.put("newsSiteUrl","http://"+siteRecord.get("domain").toString());
		record.put("newsSiteName", siteRecord.get("name"));
		int siteId = Integer.parseInt(site_id);
		return record;
	}
	
	public Map detailData(Map param, HttpServletRequest request) {
		int news_id=Integer.parseInt((String) (param.get("news_id").equals(null)?"":param.get("news_id")));
		Map record=this.getJdbcDao().queryRow("select * from "+DBTable.news+" where id=?",new Object[]{news_id});
		if(record==null) {
			return null;
		}
		
		String site_id = record.get("site_id").toString();
		Map siteRecord=this.getJdbcDao().queryRow("select id,name,site_alias,domain from "+DBTable.site+" where id=?",new Object[]{site_id});
		String site_alias=siteRecord.get("site_alias").toString();
		//获得当前语言版本
		Map localSite=(Map)request.getAttribute("localSite");
		if(localSite!=null && localSite.get("language")!=null){
			String language=localSite.get("language").toString();
			siteRecord=this.getJdbcDao().queryRow("select id,name,site_alias,domain from "+ DBTable.site +
					" where site_alias=? and language=?",new Object[]{site_alias,language});
		}
		record.put("newsSiteName", siteRecord.get("name"));

		String link=request.getScheme()+"://"+siteRecord.get("domain").toString();	
		if(request.getServerPort()!=80)
			link+=":"+request.getServerPort();
		link+=request.getContextPath();
		record.put("newsSiteUrl",link);
		
		return record;
	}
	
	/*public String getLink(Map param) {
		String siteUrlPath=Config.getSiteUrlPath();
		if(param.get("link_url")!=null && !"".equals(param.get("link_url"))) {
			return param.get("link_url").toString();
		}
		int id= new Integer( param.get("id").toString());
		int menuId = new Integer(param.get("menu_id").toString());
		Map menuMap = this.getJdbcDao().queryRow("select * from "+DBTable.menu+" where id=?",new Object[] {menuId});
		if("案例".equals(menuMap.get("name").toString())) {
			return siteUrlPath+"/caseinfo_"+id;
		}
		return siteUrlPath+"/newsdetail_" +id ;
	}*/

	
	//=================根据站点,menu_id添加新闻测试数据=================
	
	public void addNewsData(int takeSiteId,int putSiteId){
		List takeNewsList=this.getJdbcDao().querySql("select * from "+DBTable.news+" where site_id = ?",new Object[]{takeSiteId});
		if(takeNewsList != null && takeNewsList.size()>0){
			for(int i=0;i<takeNewsList.size();i++){
				Map news=(Map)takeNewsList.get(i);
				
				/*String largeImage=news.get("large_image")==null?"":news.get("large_image").toString();
				String image=news.get("image")==null?"":news.get("image").toString();
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();*/
				news.put("site_id", putSiteId);
				news.remove("id");
			/*	this.getJdbcDao().insertSql("insert into "+DBTable.news+"(menu_id,site_id,source_id,name,large_image,image,keywords,description,content,author,source,position,publish,create_time,update_time"
						+") values ("+Integer.parseInt(news.get("menu_id").toString())+","+putSiteId+","+Integer.parseInt(news.get("source_id").toString())+","+news.get("name")+","+largeImage+","+image
						+","+news.get("keywords")+","+news.get("description")+","+news.get("content")+",\"test\","+news.get("source")+","+Integer.parseInt(news.get("position").toString())+",1,"+sdf.format(date)+","+sdf.format(date)+")");*/
				int abc=this.getJdbcDao().addObj(DBTable.news, news);
				System.out.println(abc);
			}
		}
	}
	

	
	/*public List categoryListData(Map param, HttpServletRequest request) {
		List sqlParamList=new ArrayList();
		
		String sql="select id,name,image,large_image,video,video_code,create_time,source,hits,description,link_url from "+DBTable.news+" where 1 ";
	
		if(param.get("alias")!=null){
			
			String alias=param.get("alias").toString();
			//将模板的tpl_flag参数转换为menu_id
			Map site=(Map)request.getAttribute("localSite");
			if(site!=null && site.get("language")!=null){
				
				String langeuage=site.get("language").toString();
				if(langeuage.equals("TraditionalChinese"))
					langeuage="SimplifiedChinese";
				//获取同语言所有站点
				List siteList=this.getJdbcDao().querySql("select id from "+DBTable.site+" where language=?", new Object[]{langeuage});
				StringBuffer tmpStr=new StringBuffer();
				if(siteList!=null)
					for(int i=0;i<siteList.size();i++){
						Map tmpSite=(Map)siteList.get(i);
						if(i<siteList.size()-1)
							tmpStr.append(tmpSite.get("id")+",");
						else
							tmpStr.append(tmpSite.get("id"));
					}
				else
					tmpStr.append("0");
				//查询匹配的栏目
				//LogTool.info("select id from "+DBTable.menu+" where alias='"+alias+"' and site_id in ("+tmpStr.toString()+")", this.getClass());
				List tmpMenuList=this.getJdbcDao().querySql("select id from "+DBTable.menu+" where alias=? and site_id in ("+tmpStr.toString()+") and module='news'", new Object[]{alias});
				
				tmpStr=new StringBuffer();
				if(tmpMenuList!=null && tmpMenuList.size()>0)
					for(int i=0;i<tmpMenuList.size();i++){
						Map tmpMenu=(Map)tmpMenuList.get(i);
						if(i<tmpMenuList.size()-1)
							tmpStr.append(tmpMenu.get("id")+",");
						else
							tmpStr.append(tmpMenu.get("id"));
					}
				else
					tmpStr.append("0");
				sql+="and menu_id in("+tmpStr.toString()+") ";
					
				
			}else{
				return null;
			}
		}
		
		//如果需要图片
		if(param.get("site_id")!=null&& !param.get("site_id").equals("")){
			sql+=" and site_id =? ";
			sqlParamList.add(param.get("site_id"));
		}
		
		//图片
		if(param.get("image")!=null){
			int image=new Integer(param.get("image").toString());
			if(image>0){
				sql+=" and (image is not null and image!='') ";
			}else{
				sql+=" and (image is null or image='') ";
			}	
		}
		
		//再次查询过滤重复id
		if(param.get("no_ids")!=null && !param.get("no_ids").equals("")){
			sql+=" and id not in ("+param.get("no_ids")+")  ";
		}
		
		//查询推荐位置新闻
		if (param.get("index_recommend")!=null&& !param.get("recolocation").equals("")) {
			sql+=" and index_recommend = ?  ";
			sqlParamList.add(param.get("index_recommend"));
		}
		
		//查询推荐位置新闻
		if (param.get("recommend")!=null&& !param.get("recommend").equals("")) {
			sql+=" and recommend = ?  ";
			sqlParamList.add(param.get("recommend"));
		}
		
		
		sql+=" and del_flag=0 and publish=1 ";
		
		if(param.get("order")!=null&& !param.get("order").equals("")){
			sql+=" order by "+param.get("order")+",create_time desc,id desc";
		}else{
			sql+=" order by recommend desc , position asc ,create_time desc,id desc";
		}
		
		int num=15;
		if(param.get("num") != null){
			num=new Integer(param.get("num").toString());
			sql+=" limit 0,?";
			sqlParamList.add(num);
		}

		
		
		if(dataList!=null && dataList.size() > 0){
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				row.put("link", this.getLink(row));
			}
		}
		
		return dataList;
	}*/

	
	/*public List categoryPageListData(Map param, HttpServletRequest request) {
		int page=1;
		if(param.get("page")!=null){
			page=new Integer(param.get("page").toString());
		}
		int pagesize=10;//默认10
		if(param.get("pagesize")!=null){
			pagesize=new Integer(param.get("pagesize").toString());
		}
		
		
		List sqlParamList=new ArrayList();
		
		String sql="select id,name,image,video,video_code,source,create_time,description,link_url from "+DBTable.news+" where 1 ";
	
		if(param.get("alias")!=null){
			
			String alias=param.get("alias").toString();
			//将模板的tpl_flag参数转换为menu_id
			Map site=(Map)request.getAttribute("localSite");
			if(site!=null && site.get("language")!=null){
				
				String langeuage=site.get("language").toString();
				if(langeuage.equals("TraditionalChinese"))
					langeuage="SimplifiedChinese";
				//获取同语言所有站点
				List siteList=this.getJdbcDao().querySql("select id from "+DBTable.site+" where language=?", new Object[]{langeuage});
				StringBuffer tmpStr=new StringBuffer();
				if(siteList!=null)
					for(int i=0;i<siteList.size();i++){
						Map tmpSite=(Map)siteList.get(i);
						if(i<siteList.size()-1)
							tmpStr.append(tmpSite.get("id")+",");
						else
							tmpStr.append(tmpSite.get("id"));
					}
				else
					tmpStr.append("0");
				//查询匹配的栏目
				//LogTool.info("select id from "+DBTable.menu+" where alias='"+alias+"' and site_id in ("+tmpStr.toString()+")", this.getClass());
				List tmpMenuList=this.getJdbcDao().querySql("select id from "+DBTable.menu+" where alias=? and site_id in ("+tmpStr.toString()+") and module='news'", new Object[]{alias});
				
				tmpStr=new StringBuffer();
				if(tmpMenuList!=null && tmpMenuList.size()>0)
					for(int i=0;i<tmpMenuList.size();i++){
						Map tmpMenu=(Map)tmpMenuList.get(i);
						if(i<tmpMenuList.size()-1)
							tmpStr.append(tmpMenu.get("id")+",");
						else
							tmpStr.append(tmpMenu.get("id"));
					}
				else
					tmpStr.append("0");
				sql+="and menu_id in("+tmpStr.toString()+") ";
					
				
			}else{
				return null;
			}
		}
		
		//如果需要
		if(param.get("site_id")!=null&& !param.get("site_id").equals("")){
			sql+=" and site_id =? ";
			sqlParamList.add(param.get("site_id"));
		}
		
		//如果有关键词搜索 
		if(param.get("keywords")!=null&& !param.get("keywords").equals("")){
			sql+=" and name like ? ";
			sqlParamList.add("%"+param.get("keywords")+"%");
		}

		
		//如果需要图片
		if(param.get("image")!=null&& !param.get("image").equals("")){
			sql+=" and image is not null and image!='' ";
		}
		//再次查询过滤重复id
		if(param.get("no_ids")!=null&& !param.get("no_ids").equals("")){
			sql+=" and id  not in ("+param.get("no_ids")+")";
		}
		//查询推荐位置新闻
		if (param.get("recolocation")!=null&& !param.get("recolocation").equals("")) {
			sql+=" and recommendlocation like ?  ";
			sqlParamList.add("%"+param.get("recolocation")+"%");
		}
		
		if (param.get("recommend")!=null&& !param.get("recommend").equals("")) {
			sql+=" and recommend =?  ";
			sqlParamList.add(param.get("recommend"));
		}
		
		sql+=" and publish=1 and del_flag=0 ";
		
		if(param.get("order")!=null&& !param.get("order").equals("")){
			sql+=" order by "+param.get("order")+",create_time desc,id desc";
		}else{
			sql+=" order by recommend desc , position asc ,create_time desc,id desc";
		}
		
		int beginNum=(page-1)*pagesize;
		int endnum=pagesize;
		sql+=" limit ?,?";
		sqlParamList.add(beginNum);
		sqlParamList.add(pagesize);
		
		
		List dataList = this.getJdbcDao().querySql(sql, sqlParamList.toArray());
		
		
		if(dataList!=null && dataList.size() > 0){
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				row.put("link", this.getLink(row));
			}
		}
		
		return dataList;
	}*/
	
	/**
	 * 获取下级分类的id，不包含当前id
	 * @param pid
	 * @return
	 */
	public List getSubCategoryIds(int pid) {
		List ids=new ArrayList();
		List list=this.getJdbcDao().querySql("select id from "+DBTable.newsCategory+" where pid=? and del_flag=0 and publish=1 ", new Object[] {pid});
		if(list!=null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				Map item=(Map)list.get(i);
				ids.add(item.get("id"));
				List subList=getSubCategoryIds(Integer.parseInt(item.get("id").toString()));
				if(subList!=null && subList.size()>0) {
					ids.addAll(subList);
				}
			}
		}
		return ids;
	}
	
	/*@Async
	public void startUpdateWeixinNewsSource() {
	    System.out.println("startUpdateWeixinNewsSource");
	}
	*/
}
